package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 添加套餐
     *
     * @param setmealDto
     * @return
     */
//    @PostMapping
//    public R<String> save(@RequestBody SetmealDto setmealDto) {
//        //调用service保存
//        setmealService.saveEhithDish(setmealDto);
//        return R.success("保存套餐成功");
//    }
    @PostMapping
    @CacheEvict(value = "setmealCache",key = "setmealDto.categoryId")
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        //添加套餐数据
        setmealService.saveWithDish(setmealDto);
        return R.success("添加套餐成功");
    }

    @GetMapping("/page")
    public R<Page<SetmealDto>> page(int page, int pageSize, String name) {
        //一：分类的分页查询
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);

        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(name), Setmeal::getName, name);
        lqw.orderByDesc(Setmeal::getUpdateTime);

        setmealService.page(setmealPage, lqw);
        //二：创建Page<SetmealDto>分页对象
        Page<SetmealDto> dtoPage = new Page<>();
        //复制分页数据
        BeanUtils.copyProperties(setmealPage, dtoPage, "records");

        //处理分页数据
        List<Setmeal> records = setmealPage.getRecords();
        List<SetmealDto> dtolist = new ArrayList<>();
        records.forEach(setmeal -> {
            SetmealDto Dto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, Dto);
            //查询分类信息
            Category category = categoryService.getById(setmeal.getCategoryId());
            if (category != null) {
                Dto.setCategoryName(category.getName());
            }
            dtolist.add(Dto);
        });
        dtoPage.setRecords(dtolist);
        return R.success(dtoPage);
    }

    /**
     * 删除套餐
     */
    @CacheEvict(value = "setmealCache",allEntries = true)
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids) {
        setmealService.removeWithDish(ids);
        return R.success("删除套餐成功");
    }

    /**
     *查询套餐集合
     */
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal) {
        //1.条件构造器
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        lqw.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());
        lqw.orderByDesc(Setmeal::getUpdateTime);

        //查询
        List<Setmeal> list = setmealService.list(lqw);
        return R.success(list);
    }
}
